//
//  THJStorePurchaseVC.m
//  THJXHW
//
//  Created by 河南德艺 on 2017/11/7.
//  Copyright © 2017年 河南德艺. All rights reserved.
// 求购信息

#import "THJStorePurchaseVC.h"
#import "JXSegment.h"
#import "JXPageView.h"
#import "THJCompanyProductView.h"
#import "THJStorePurchaseDetailVC.h"
#import "ZBNetworking.h"
#import <MJRefresh.h>
#import "THJTAdeFabuCell.h"
#import "THJProductDetailVC.h"
#import "THJSelectView.h"
@interface THJStorePurchaseVC ()<JXSegmentDelegate,UITableViewDelegate,UITableViewDataSource,THJSelectViewDelegate>
{
    JXSegment * segment;
    UITableView * _tableview;
    NSInteger pageNo;
     NSString * areaIndex;
    NSInteger selectIndex;
    NSString * catid;
}
@property (weak, nonatomic) IBOutlet UIView *btnBgView;
@property (strong,nonatomic) NSMutableArray * listArr;
@property (strong,nonatomic) NSMutableArray * titleArr;
@property (strong,nonatomic) NSMutableArray * titleIdArr;
@property (strong,nonatomic) NSMutableArray * areaArr;
@property (strong,nonatomic) THJSelectView * selectView;
@end

@implementation THJStorePurchaseVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setStateBarColor:[UIColor clearColor]];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    selectIndex = 0;
    pageNo = 1;
    areaIndex = @"1";
    [self setShadow];
    if(KIsiPhoneX){
        [self setXClassNavBarWithTitle:@"我的求购"];
        self.topSpace.constant = 120;
    }else{
        [self setNavBarWithTitle:@"我的求购"];
    }
    
    [self setRightNavBtn];
    [self setProductListView];
    [self getData];
    [self getAreaData];
    // Do any additional setup after loading the view from its nib.
}
-(THJSelectView*)selectView{
    if(!_selectView){
        _selectView = [[THJSelectView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        _selectView.dataArr = self.areaArr;
        _selectView.delegate = self;
        _selectView.listKey = @"areaname";
    }
    return _selectView;
}
-(NSMutableArray*)listArr{
    if(!_listArr){
        _listArr = [[NSMutableArray alloc] init];
    }
    return _listArr;
}
-(NSMutableArray*)titleIdArr{
    if(!_titleIdArr){
        _titleIdArr = [[NSMutableArray alloc] init];
    }
    return _titleIdArr;
}
-(NSMutableArray*)titleArr{
    if(!_titleArr){
        _titleArr = [[NSMutableArray alloc] init];
    }
    return _titleArr;
}
-(void)setTitleMenuWithTitleArr:(NSArray*)arr{
    CGFloat constant;
    if(KIsiPhoneX){
        constant = 74;
    }else{
        constant = 64;
    }
    segment = [[JXSegment alloc] initWithFrame:CGRectMake(0, constant, ScreenWidth, 45)];
    segment.scrollView.backgroundColor = COLOR(83, 84, 85, 1);
    [segment updateChannels:arr];
    segment.delegate = self;
    [self.view addSubview:segment];
}
//产品列表
-(void)setProductListView{
    CGFloat constant;
    if(KIsiPhoneX){
        constant = 175;
    }else{
        constant = 165;
    }
    _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, constant, ScreenWidth, ScreenHeight - constant) style:UITableViewStylePlain];
    [_tableview registerNib:[UINib nibWithNibName:@"THJTAdeFabuCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    _tableview.delegate = self;
    _tableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    _tableview.tableFooterView = [UIView new];
    _tableview.rowHeight = 145;
    _tableview.dataSource = self;
    [self.view addSubview:_tableview];
}
//下拉加载
-(void)loadMore{
    pageNo++;
    
    [self getDataWithType:catid PageNo:pageNo Area:areaIndex];
}
#pragma mark - tableviewDelegate --
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.listArr.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary * dic = self.listArr[indexPath.row];
    THJTAdeFabuCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.titleLab.text = dic[@"title"];
    cell.addressLab.text = dic[@"address"];
    cell.dateLab.text = [self timeChangeWithString:dic[@"addtime"]];
    cell.companLab.text = dic[@"company"];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary * dic = self.listArr[indexPath.row];
    THJStorePurchaseDetailVC * vc = [THJStorePurchaseDetailVC new];
    vc.itemid = dic[@"itemid"];
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)setShadow{
    self.btnBgView.layer.shadowColor = [UIColor lightGrayColor].CGColor;//设置阴影的颜色
    self.btnBgView.layer.shadowOpacity = 0.8;//设置阴影的透明度
    self.btnBgView.layer.shadowOffset = CGSizeMake(5, 5);//设置阴影的偏移量
    self.btnBgView.layer.shadowRadius = 3;//设置阴影的圆角
}
//获取滚动顶部菜单
-(void)getData{
    NSString * url = [NSString stringWithFormat:@"%@%@",THJUrlDomain,@"index.php?f=category&r=category&m=top&mid=6"];
    __weak typeof(self) weakSelf = self;
    [ZBRequestManager requestWithConfig:^(ZBURLRequest *request){
        request.urlString = url;
        request.apiType = ZBRequestTypeCache;
    }  success:^(id responseObject,apiType type){
        NSDictionary * receiveData = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"top-%@",receiveData);
//        weakSelf.titleArr = receiveData[@"data"];
      
//        NSMutableArray * arr = [NSMutableArray array];
        for (NSDictionary * dic in receiveData[@"data"]) {
            [weakSelf.titleArr addObject:dic[@"catname"]];
            [weakSelf.titleIdArr addObject:dic[@"catid"]];
        }
        [weakSelf.titleArr insertObject:@"全部" atIndex:0];
        [weakSelf.titleIdArr insertObject:@"" atIndex:0];
        [self setTitleMenuWithTitleArr:weakSelf.titleArr];
        [self getDataWithType:weakSelf.titleIdArr[0] PageNo:1 Area:@""];
    } failed:^(NSError *error){
        
    }];
}
//获取地区数据
-(void)getAreaData{
    NSString * url = [NSString stringWithFormat:@"%@%@%@",THJUrlDomain,THJindexPHP,@"f=Area&r=Area"];
    __weak typeof(self) weakSelf = self;
    [THJNetWork GetDataWithGetRequestTypeUrl:url Callback:^(NSDictionary* receiveData, NSError *error) {
        NSLog(@"rece-%@",receiveData);
        weakSelf.areaArr = [receiveData[@"data"] mutableCopy];
        NSDictionary * dic = @{@"areaid":@"0",@"areaname":@"全国"};
        [weakSelf.areaArr insertObject:dic atIndex:0];
    }];
}

/**
 获取数据
 */
-(void)getDataWithType:(NSString*)type PageNo:(NSInteger)pageNo Area:(NSString*)area{
    NSLog(@"求购平台");
    [self SVHUDShow];
    __weak typeof(self) weakself = self;
    [ZBRequestManager requestWithConfig:^(ZBURLRequest *request){
        request.urlString=[NSString stringWithFormat:@"%@%@f=buy_6&r=buy_6&catid=%@&area=%@&page=%ld",THJUrlDomain,THJindexPHP,type,area,pageNo];
        NSLog(@"求购列表-url--%@",request.urlString);
        request.apiType=ZBRequestTypeCache;
    }  success:^(id responseObject,apiType type){
        NSDictionary * receiveData = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"求购数据--%@",receiveData);
        [_tableview.mj_footer endRefreshing];
        [weakself SVHUDHiden];
        if([receiveData[@"status"] isEqual:@"200"]){
//            NSDictionary  * dataDic = receiveData[@"data"];
            for (NSDictionary * dic in receiveData[@"data"][@"buy_list"]) {
                [self.listArr addObject:dic];
            }
            
            [_tableview reloadData];
        }
    } failed:^(NSError *error){
        
    }];
}

#pragma mark - JXPageViewDataSource
-(NSInteger)numberOfItemInJXPageView:(JXPageView *)pageView{
    return 7;
}
-(UIView*)pageView:(JXPageView *)pageView viewAtIndex:(NSInteger)index{
    UIView *view = [[UIView alloc] init];
    return view;
}
#pragma mark - 分类选择 -
-(void)JXSegment:(JXSegment*)segment didSelectIndex:(NSInteger)index{
    selectIndex = index;
    [self.listArr removeAllObjects];
    catid = self.titleIdArr[index];
    [self getDataWithType:catid PageNo:1 Area:@""];
}
#pragma mark - JXPageViewDelegate -
- (void)didScrollToIndex:(NSInteger)index{
    [segment didChengeToIndex:index];
    NSLog(@"**-%ld",(long)index);
}
#pragma mark - 地区选择 -
-(void)selectIndex:(NSInteger)index{
    if(index != 999999){
        [self.listArr removeAllObjects];
        NSLog(@"area-index-%ld",index);
        [self setStateBarColor:[UIColor clearColor]];
        NSDictionary * dic = self.areaArr[index];
        [self.btn2 setTitle:dic[@"areaname"] forState:UIControlStateNormal];
        areaIndex = dic[@"areaname"];
        [self getDataWithType:catid PageNo:1 Area:areaIndex];
    }
    
}
//种类选择
- (IBAction)qunbuZhongleiAction:(id)sender {
}
//地区选择
- (IBAction)areaSelectAction:(id)sender {
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
//    [self setStateBarColor:SingleColor(clearColor)];
    [window addSubview:self.selectView];
}
- (IBAction)backBtnAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
@end
